﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using EPiServer;

namespace TemplateFoundation.UI
{
    /// <summary>
    /// Used to create admin and edit plugin pages based on native EPiServer appearance
    /// </summary>
    public abstract class SystemPageBase : EPiServer.UI.SystemPageBase
    {
        /// <summary>
        /// Sets the master file of the system page
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            // Use the EPiServer native master page for UI pages
            MasterPageFile = UriSupport.InternalUIUrl + "MasterPages/EPiServerUI.Master";
        }

        /// <summary>
        /// Initializes the <see cref="T:System.Web.UI.HtmlTextWriter"/> object and calls on the child controls of the <see cref="T:System.Web.UI.Page"/> to render.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> that receives the page content.</param>
        protected override void Render(HtmlTextWriter writer)
        {
            // Render the page title as the heading of the system page using native EPiServer appearance
            if (!string.IsNullOrEmpty(Title))
            {
                writer.AddAttribute("class", "epi-contentContainer epi-padding");
                writer.RenderBeginTag("div");

                writer.AddAttribute("class", "epi-contentArea");
                writer.RenderBeginTag("div");

                new HtmlGenericControl("h1") {InnerText = Title }.RenderControl(writer);

                base.Render(writer);

                writer.RenderEndTag();

                writer.RenderEndTag();
            }
        }
    }
}
